//SQRT декомпозиция
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 50003;
int n;
int  a[maxn], b[maxn];
const int h = 800;
 
 
int lesss(int l, int r, int x)
{
   if (l<r)
   {
      int m=(l+r)/2;
      if (b[m]<x) return lesss(m+1,r,x);
      else if (b[m]>x) return lesss(l,m,x);
      else return m+1; 
   }
   else
   {
      return r;
   }
}
 
int ld(int x, int p)
{
   int s = 0;
   int i;
   for (i=0; i+h<=p; i+=h)
      s += lesss(i,i+h,x) - i;
   for (int j = i; j<p; j++) 
      if (a[j]<=x) s++;
   return s;
}
 
int main()
{
   int n;
   cin>>n;
   for (int i=0; i<n; i++)
   {
      cin>>a[i];
      b[i] = a[i];
   }
   //sort parts
   for (int i=0; i+h<=n; i+=h)
      sort(b+i,b+(i+h));
   int k;
   cin>>k;
   for (int i=0; i<k; i++)
   {
      int ind, r;
      cin>>ind>>r;
      ind--;
      cout<<ld(a[ind],ind+r+1 < n ? ind+r+1:n)-ld(a[ind],ind-r > 0 ? ind-r : 0) - 1<<endl;
   }
   return 0;
}